package com.bigShen.mybatis.official;

import com.bigShen.mybatis.official.dao.InstrumentInfoMapper;
import com.bigShen.mybatis.official.entity.InstrumentInfo;
import com.bigShen.mybatis.official.entity.InstrumentInfoKey;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author eddy.shen
 * @Date 2018/12/15 20:40
 **/
public class MyBatisCachMain {

    static SqlSessionFactory getSqlSessionFactory() throws IOException {

        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        return sqlSessionFactory;

    }

    /**
     * @param args
     * @throws IOException
     *  mybatis一级缓存,二级缓存
     *  缓存作用?
     *  怎么验证?
     *  有没有问题?
     */
    public static void main(String[] args) throws IOException {

        String iCode = "111790114";
        String aType = "SPT_BD";
        String mType = "X_CNBD";

        Long start = System.currentTimeMillis();
        SqlSession firstSession = getSqlSessionFactory().openSession();
        InstrumentInfoMapper instrumentInfoMapper = firstSession.getMapper(InstrumentInfoMapper.class);
        InstrumentInfo firstInfo = instrumentInfoMapper.selectByPrimaryKey(iCode, aType, mType);
        System.out.println(firstInfo);
        Long end = System.currentTimeMillis();
        System.out.println("first cost : " + (end - start));

//        start = System.currentTimeMillis();
//        firstInfo = instrumentInfoMapper.selectByPrimaryKey(iCode, aType, mType);
//        System.out.println(firstInfo);
//        end = System.currentTimeMillis();
//        System.out.println("second cost : " + (end - start));

        start = System.currentTimeMillis();
        SqlSession secondSession = getSqlSessionFactory().openSession();
        InstrumentInfoMapper secondMapper = secondSession.getMapper(InstrumentInfoMapper.class);
        InstrumentInfo secondInfo = secondMapper.selectByPrimaryKey(iCode, aType, mType);
        System.out.println(secondInfo);
        end = System.currentTimeMillis();
        System.out.println("second cost : " + (end - start));

        start = System.currentTimeMillis();
        SqlSession thirdSession = getSqlSessionFactory().openSession();
        InstrumentInfoMapper thirdMapper = thirdSession.getMapper(InstrumentInfoMapper.class);
        InstrumentInfo thirdInfo = thirdMapper.selectByPrimaryKey(iCode, aType, mType);
        System.out.println(thirdInfo);
        end = System.currentTimeMillis();
        System.out.println("third cost : " + (end - start));

    }




}
